package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {


    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private DishService  dishService;

    /**\
     *新增套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R save(@RequestBody SetmealDto setmealDto){
        R r = setmealService.addSetmeal(setmealDto);
        return r;
    }
    /**
     * 套餐分页查询
     */

    @GetMapping("/page")
    public R page(int page,int pageSize,String name){
        //创建分页构造器
        Page pageInfo = new Page(page,pageSize);
        //创建条件构造器
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        //添加条件
        qw.like(StringUtils.isNotEmpty(name),Setmeal::getName,name);
        qw.orderByDesc(Setmeal::getUpdateTime);
        pageInfo = setmealService.page(pageInfo, qw);
        //这里不能直接返回数据
        //因为还要显示套餐名称，这时候如果返回就是返回的categoryId
        //要根据categoryId到category表中查询到分类名称
        ArrayList<SetmealDto> setmealDtos = new ArrayList<>();
        List<Setmeal> records = pageInfo.getRecords();
        for (Setmeal setmeal : records) {
            Long categoryId = setmeal.getCategoryId();
            Category category= categoryService.getById(categoryId);
            String categoryName = category.getName();
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal,setmealDto);
            setmealDto.setCategoryName(categoryName);
            setmealDtos.add(setmealDto);
        }
        HashMap map = new HashMap<>();
        map.put("total",pageInfo.getTotal());
        map.put("records",setmealDtos);

         return R.success(map);
    }

    /**
     * 删除套餐
     * 删除
     *
     */
//
    @DeleteMapping
    public R delete(@RequestParam List<Long> ids){
        R r = setmealService.delete(ids);
        return r;
    }
    @GetMapping("/{id}")
    public R<SetmealDto> getById(@PathVariable Long id){
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(Setmeal::getId,id);
        Setmeal setmeal = setmealService.getOne(qw);

        LambdaQueryWrapper<SetmealDish> qw2 = new LambdaQueryWrapper<>();
        qw2.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(qw2);

        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        setmealDto.setSetmealDishes(list);
        return R.success(setmealDto);
    }


    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){

        //先更新套餐
        setmealService.updateById(setmealDto);

        //再更新套餐下的菜品
        //先删除setmealId下的所有菜品
        Long id = setmealDto.getId();
        //先查询出来
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(qw);
        //再删除
        for (SetmealDish setmealDish : list) {
            Long id1 = setmealDish.getId();
            setmealDishService.removeById(id1);
        }
        //再更新setmealDish
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(id);
            setmealDishService.save(setmealDish);
        }
        return R.success("修改套餐成功！");
    }

    /**
     * 停售起售
     */

    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable Integer status,@RequestParam List<Long> ids){
        LambdaUpdateWrapper<Setmeal> up = new LambdaUpdateWrapper<>();
        up.set(Setmeal::getStatus,status);
        up.in(Setmeal::getId,ids);
        boolean update = setmealService.update(up);
        for (Long id : ids) {
            Dish dish = new Dish();
            dish.setId(id);
            dish.setStatus(status);
            dishService.updateById(dish);
        }
        return update?R.success("操作成功！"):R.error("操作失败!");
    }

    /**
     *根据套餐分类id查询该套餐分类下的套餐
     * @return
     */
    @GetMapping("/list")
    public R<List<Setmeal>> getSetmealByCategory(Category category){
        Long categoryId = category.getId();

        //select * from setmeal where category_id=?
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(Setmeal::getCategoryId,categoryId);
        qw.eq(Setmeal::getStatus,1);
        List<Setmeal> setmeals = setmealService.list(qw);
        return setmeals!=null?R.success(setmeals):R.error("查询失败");
    }







}
